

import formula.calculator.R;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.Activity;

public class Voltage extends Activity{

        
        private double answer = 0;
        private EditText voltage;
        private String sVoltage;
        private EditText current;
        private String sCurrent;
        private EditText resistance;
        private String sResistance;
        private TableLayout tl;
        private TableRow row;
        private TextView tv;
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voltage_activity);
        tl = (TableLayout)findViewById(R.id.voltageLayout);
        row = new TableRow(this);
        tv = new TextView(this);
    }
        
        public void onClick(View v) {
                switch(v.getId()) {
                        case R.id.voltageSolve:
                                solveForVoltage();
                                break;
                }
        }
        
        
        private void solveForVoltage() {
                voltage = (EditText)findViewById(R.id.voltageValue);
                current = (EditText)findViewById(R.id.currentValue);
                resistance = (EditText)findViewById(R.id.resistanceValue);
                sVoltage = voltage.getText().toString();
                sCurrent = current.getText().toString();
                sResistance = resistance.getText().toString();
                
                double voltageNum = 0;
                double currentNum = 0;
                double resistanceNum = 0;
                
                if(!sVoltage.equals("")) {
                        voltageNum = Integer.parseInt(sVoltage);
                }
                if(!sCurrent.equals("")) {
                        currentNum = Integer.parseInt(sCurrent);
                }
                if(!sResistance.equals("")) {
                        resistanceNum = Integer.parseInt(sResistance);
                }
                
                if(sVoltage.equals("")) {
                        voltage(currentNum, resistanceNum);
                        tv.setText("Voltage: " + answer);
                } else if(sCurrent.equals("")) {
                        current(voltageNum, resistanceNum);
                        tv.setText("Current: " + answer);
                } else if(sResistance.equals("")) {
                        resistance(voltageNum, currentNum);
                        tv.setText("Resistance: " + answer);
                }
        tl.addView(row);
        row.addView(tv);
        }

        private void voltage(double current, double resistance) {
                double result = current * resistance;
                answer = result;
        }
        
        private void current(double voltage, double resistance) {
                double result = voltage / resistance;
                answer = result;
        }
        
        private void resistance(double voltage, double current) {
                double result = voltage / current;
                answer = result;
        }
}